/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/vab/layouts'
import { publicPath, routerMode } from '@/config'

Vue.use(VueRouter)
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/',
    name: 'Root',
    component: Layout,
    meta: {
      title: '首页',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/index'),
        meta: {
          title: '首页',
          noClosable: true,
        },
      },
    ],
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/403'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404'),
    meta: {
      hidden: true,
    },
  },
]

export const asyncRoutes = [
  {
    path: '/car',
    name: 'Car',
    component: Layout,
    meta: {
      title: 'CAN',
      icon: 'police-car-line',
    },
    children: [
      {
        path: 'index',
        name: 'CarIndex',
        component: () => import('@/views/car/index'),
        meta: {
          title: '车辆品牌',
        },
      },
      {
        path: 'type',
        name: 'CarType',
        component: () => import('@/views/car/type/index'),
        meta: {
          title: '车辆类型',
        },
      },
      {
        path: 'publicPlay',
        name: 'PublicPlay',
        component: () => import('@/views/car/publicPlay/index'),
        meta: {
          title: '公共功能',
        },
      },
      {
        path: 'play',
        name: 'Play',
        component: () => import('@/views/car/play/index'),
        meta: {
          title: '私有功能',
        },
      },
    ],
  },
  {
    path: '/user',
    name: 'User',
    component: Layout,
    meta: {
      title: '用户',
      icon: 'account-pin-box-line',
    },
    children: [
      {
        path: '/list',
        name: '/list',
        component: () => import('@/views/user'),
        meta: {
          title: '用户列表',
        },
      },
      {
        path: '/device',
        name: '/device',
        component: () => import('@/views/userDevice'),
        meta: {
          title: '设备列表',
        },
      },
      {
        path: '/led',
        name: 'LedDevice',
        component: () => import('@/views/ledDevice'),
        meta: {
          title: 'LED列表',
        },
      },
      {
        path: '/action',
        name: '/action',
        component: () => import('@/views/user/action'),
        meta: {
          title: '操作记录',
        },
      },
      {
        path: '/feedBack',
        name: 'FeedBack',
        component: () => import('@/views/feedBack'),
        meta: {
          title: '意见反馈',
        },
      },
    ],
  },
  {
    path: '/admin',
    name: 'Admin',
    component: Layout,
    meta: {
      title: '账号',
      icon: 'user-settings-line',
    },
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/admin'),
        meta: {
          title: '管理员管理',
        },
      },
    ],
  },
  {
    path: '/setting',
    name: 'Setting',
    component: Layout,
    meta: {
      title: '配置',
      icon: 'settings-2-line',
    },
    children: [
      {
        path: 'device',
        name: 'Device',
        component: () => import('@/views/device'),
        meta: {
          title: '设备管理',
        },
      },
      {
        path: 'ledMaterial',
        name: 'LedMaterial',
        component: () => import('@/views/ledMaterial'),
        meta: {
          title: 'LED素材',
        },
      },
      {
        path: 'pushNotice',
        name: 'PushNotice',
        component: () => import('@/views/pushNotice'),
        meta: {
          title: '消息推送',
        },
      },
      {
        path: 'attachment',
        name: 'Attachment',
        component: () => import('@/views/attachment'),
        meta: {
          title: '附件管理',
        },
      },
      // {
      //   path: 'menuManagement',
      //   name: 'MenuManagement',
      //   component: () => import('@/views/setting/menuManagement'),
      //   meta: {
      //     title: '菜单管理',
      //     icon: 'menu-2-fill',
      //   },
      // },
      {
        path: 'treaty',
        name: 'Treaty',
        component: () => import('@/views/treaty'),
        meta: {
          title: '协议管理',
        },
      },
      {
        path: 'settingManagement',
        name: 'SettingManagement',
        component: () => import('@/views/setting/settingManagement'),
        meta: {
          title: '系统设置',
          noKeepAlive: 'true',
        },
      },
    ],
  },
  {
    path: '*',
    redirect: '/404',
    meta: {
      hidden: true,
    },
  },
]

const router = createRouter()

function fatteningRoutes(routes) {
  return routes.flatMap((route) => {
    return route.children ? fatteningRoutes(route.children) : route
  })
}

export function resetRouter(routes = constantRoutes) {
  routes.map((route) => {
    if (route.children) {
      route.children = fatteningRoutes(route.children)
    }
  })
  router.matcher = createRouter(routes).matcher
}

function createRouter(routes = constantRoutes) {
  return new VueRouter({
    base: publicPath,
    mode: routerMode,
    scrollBehavior: () => ({
      y: 0,
    }),
    routes: routes,
  })
}

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

export default router
